#!/usr/bin/env python
# coding=utf-8

from pymongo import MongoClient
import tushare as ts
import numpy as np
import pandas as pd
import datetime
import json
import os

# 每天更新股票基本面数据的处理脚本
def get_db():
    client = MongoClient("localhost", 27017)
    db = client.stock
    return db

def get_lastday(db):
    data = db.stocks_basics.find({"code":"000001"}).sort([("date", -1)]).limit(1)
    data = list(data)
    lastday = data[0]['date']
    print("lastday = ", lastday)
    return lastday

if __name__ == "__main__":
    db = get_db()
    print(db)

    day = pd.Timestamp(get_lastday(db))
    today = pd.Timestamp(str(datetime.date.today()))
    day = day + datetime.timedelta(days=1)

    while day <= today:
        print("find date " + day.strftime('%Y-%m-%d'))
        t = db.stocks.find_one({'code': 'hs300', 'date':day.strftime('%Y-%m-%d')})
        if t != None:
            print("handle date " + day.strftime('%Y-%m-%d'))
            try:
                data = ts.get_stock_basics(date=day.strftime('%Y-%m-%d'))
            except:
                print("404 " + day.strftime('%Y-%m-%d'))
                pass
            else:
                if not data.empty:
                    data['date'] = day.strftime('%Y-%m-%d')
                    data['code'] = data.index
                    db.stocks_basics.insert(json.loads(data.to_json(orient='records', date_format='iso')))
                    # print(day.strftime('%Y-%m-%d'))

        day = day + datetime.timedelta(days=1)
